/**
 * MenuController.java com.lianniu.mgr.controller
 *
 * Function： TODO
 *
 * ver date author ────────────────────────────────── 0.1 2016年3月24日 王天锦
 *
 * Copyright (c) 2016, TNT All Rights Reserved.
 */

package com.lianniu.mgr.baseService.menu.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.hibernate.validator.internal.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.menu.model.MenuSysVo;
import com.lianniu.mgr.baseService.menu.model.MenuVo;
import com.lianniu.mgr.baseService.menu.service.IMenuService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;

/**
 * ClassName:MenuController
 *

 * @version
 * @since Ver 0.1
 * @Date 2016年3月24日 下午2:01:24
 */
@Controller
@RequestMapping("/rest/menu/")
public class MenuRestController {

  private final static Logger logger = Logger.getLogger(MenuRestController.class);
  @Autowired
  private IMenuService menuService;

  /**
   * 
   * menuList:菜单列表
   * 
   * @param @param request
   * @param @param dict
   * @param @param paginationInfo
   * @param @return 设定文件
   * @return BaseResult DOM对象
   * @throws
   */
  @RequestMapping(value = {"/menuList"}, method = {RequestMethod.GET, RequestMethod.POST})
  public @ResponseBody BaseResult menuList(HttpServletRequest req) {
    logger.info("MenuController menuList begin.");
    String sysId = req.getParameter("sysId");
    logger.info("MenuController menuList sysId:" + JSON.toJSONString(sysId));

    try {

      logger.info("getMenuAll start.");
      BaseResult baseResult = menuService.getMenuBySysId(sysId);
      logger.info("getMenuAll baseResult:" + JSON.toJSONString(baseResult));
      logger.info("getMenuAll end.");

      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
        logger.info("DictRestController getMenuAll end.");
        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
      }

      List<MenuVo> list = (List<MenuVo>) baseResult.getData();

      if (list == null || list.size() == 0) {
        logger.info("MenuController getMenuAll end.");
        return BaseResult.fail("0002", "未查询到值");
      }

      logger.info("MenuController getMenuAll end.");
      return BaseResult.success(list);

    } catch (Exception e) {
      logger.error("MenuController menuList error:" + e.getMessage());
      logger.info("MenuController menuList end.");
      return BaseResult.exception(e.getMessage());
    }

  }


  @RequestMapping(value = {"/getsysinfo"}, method = RequestMethod.GET)
  public @ResponseBody BaseResult getSysInfo(HttpServletRequest request) {
    logger.info("MenuController getSysInfo begin.");

    Map res = new HashMap();
    try {

      logger.info("getSysInfo start.");
      BaseResult baseResult = menuService.getSysInfoMap();
      logger.info("getSysInfoMap baseResult:" + JSON.toJSONString(baseResult));
      logger.info("getSysInfoMap end.");

      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
        logger.info("DictRestController getsysinfo end.");
        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
      }

      List<MenuSysVo> list = (List<MenuSysVo>) baseResult.getData();

      if (list == null || list.size() == 0) {
        logger.info("MenuController getSysInfoMap end.");
        return BaseResult.fail("0002", "未查询到值");
      }

      logger.info("MenuController getSysInfoMap end.");
      return BaseResult.success(list);

    } catch (Exception e) {
      logger.error("MenuController getSysInfo error:" + e.getMessage());
      logger.info("MenuController getSysInfo end.");
      return BaseResult.exception(e.getMessage());
    }
  }


  /**
   * 
   * menuList:菜单列表
   * 
   * @param @param request
   * @param @param dict
   * @param @param paginationInfo
   * @param @return 设定文件
   * @return BaseResult DOM对象
   * @throws
   */
  @RequestMapping(value = {"/menuAdd"}, method = {RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult menuAdd(HttpServletRequest req,MenuVo menuVo ) {
    logger.info("MenuController menuAdd begin.");
    // MenuVo menuVo=(MenuVo)req.getParameterMap();
    /*MenuVo menuVo = new MenuVo();
    String sysId = req.getParameter("sysId");
    if (!StringUtil.isEmpty(sysId)) {
      menuVo.setSysId(sysId);
    }*/
    String level = req.getParameter("levelParent");
    if (!StringUtil.isEmpty(level)) {
      String[] levelArr = level.split("-");
      menuVo.setLevel(Integer.valueOf(levelArr[0]) + 1);
      menuVo.setParentId(levelArr[1]);
    }
    /*String menuName = req.getParameter("menuName");
    if (!StringUtil.isEmpty(menuName)) {
      menuVo.setMenuName(menuName);
    }
    String menuUrl = req.getParameter("menuUrl");
    if (!StringUtil.isEmpty(menuUrl)) {
      menuVo.setMenuUrl(menuUrl);
    }
    String sortNum = req.getParameter("sortNum");
    if (!StringUtil.isEmpty(sortNum)) {
      menuVo.setSortNum(Integer.valueOf(sortNum));
    }
    String menuDesc = req.getParameter("menuDesc");
    if (!StringUtil.isEmpty(menuDesc)) {
      menuVo.setMenuDesc(menuDesc);
    }*/
    menuVo.setId(StringUtil.getUUID());
    logger.info("MenuController menuAdd paginationInfo:" + JSON.toJSONString(menuVo));

    try {

      logger.info("menuAdd start.");
      BaseResult baseResult = menuService.addMenu(menuVo);
      logger.info("menuAdd baseResult:" + JSON.toJSONString(baseResult));
      logger.info("menuAdd end.");

      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
        logger.info("DictRestController menuAdd end.");
        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
      }

      int count = (int) baseResult.getData();

      if (count == 0) {
        logger.info("MenuController menuAdd end.");
        return BaseResult.fail("0002", "新增失败");
      } else if (count == -1) {
        logger.info("MenuController menuAdd end.");
        return BaseResult.fail("0003", "排序号重复");
      }

      logger.info("MenuController menuAdd end.");
      return BaseResult.success(count);

    } catch (Exception e) {
      logger.error("MenuController menuAdd error:" + e.getMessage());
      logger.info("MenuController menuAdd end.");
      return BaseResult.exception(e.getMessage());
    }

  }


  /**
   * 
   * getParentMenuList:获取上级菜单
   * 
   * @param @param req
   * @param @return 设定文件
   * @return BaseResult DOM对象
   * @throws
   */
  @RequestMapping(value = {"/getParentMenuList"}, method = RequestMethod.GET)
  public @ResponseBody BaseResult getParentMenuList(HttpServletRequest req) {
    logger.info("MenuController getParentMenuList begin.");
    String sysId = req.getParameter("sysId");
    logger.info("MenuController getParentMenuList sysId:" + JSON.toJSONString(sysId));

    try {

      logger.info("getMenuBySysId start.");
      BaseResult baseResult = menuService.getMenuBySysId(sysId);
      logger.info("getMenuBySysId baseResult:" + JSON.toJSONString(baseResult));
      logger.info("getMenuBySysId end.");

      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
        logger.info("DictRestController getMenuBySysId end.");
        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
      }

      List<MenuVo> list = (List<MenuVo>) baseResult.getData();

      if (list == null || list.size() == 0) {
        logger.info("MenuController getMenuAll end.");
        return BaseResult.fail("0002", "未查询到值");
      }

      logger.info("MenuController getMenuBySysId end.");
      return BaseResult.success(list);

    } catch (Exception e) {
      logger.error("MenuController getParentMenuList error:" + e.getMessage());
      logger.info("MenuController getParentMenuList end.");
      return BaseResult.exception(e.getMessage());
    }

  }


  /**
   * 
   * getEditData:获取编辑数据
   * 
   * @param @param req
   * @param @return 设定文件
   * @return BaseResult DOM对象
   * @throws
   */
  @RequestMapping(value = {"/geteditdata"}, method = RequestMethod.GET)
  public @ResponseBody BaseResult getEditData(HttpServletRequest req) {
    logger.info("MenuController getEditData begin.");
    String menuId = req.getParameter("menuid");
    logger.info("MenuController getEditData menuId:" + JSON.toJSONString(menuId));

    try {

      logger.info("getMenuBySysId start.");
      BaseResult baseResult = menuService.getMenuByMenuId(menuId);
      logger.info("getMenuByMenuId baseResult:" + JSON.toJSONString(baseResult));
      logger.info("getMenuByMenuId end.");

      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
        logger.info("DictRestController getMenuByMenuId end.");
        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
      }

      MenuVo vo = (MenuVo) baseResult.getData();

      if (vo == null) {
        logger.info("MenuController getMenuByMenuId end.");
        return BaseResult.fail("0002", "未查询到值");
      }

      logger.info("MenuController getEditData end.");
      return BaseResult.success(vo);

    } catch (Exception e) {
      logger.error("MenuController getEditData error:" + e.getMessage());
      logger.info("MenuController getEditData end.");
      return BaseResult.exception(e.getMessage());
    }

  }


  /**
   * 
   * menueEditData:菜单编辑保存
   * 
   * @param @param req
   * @param @return 设定文件
   * @return BaseResult DOM对象
   * @throws
   */
  @RequestMapping(value = {"/menueditdata"}, method = {RequestMethod.POST,RequestMethod.GET})
  public @ResponseBody BaseResult menueEditData(HttpServletRequest req,MenuVo menuVo ) {
    logger.info("MenuController menueEditData begin.");
    // MenuVo menuVo=(MenuVo)req.getParameterMap();
   /* MenuVo menuVo = new MenuVo();
    String menuName = req.getParameter("menuName");
    if (!StringUtil.isEmpty(menuName)) {
      menuVo.setMenuName(menuName);
    }
    String menuUrl = req.getParameter("menuUrl");
    if (!StringUtil.isEmpty(menuUrl)) {
      menuVo.setMenuUrl(menuUrl);
    }
    String sortNum = req.getParameter("sortNum");
    if (!StringUtil.isEmpty(sortNum)) {
      menuVo.setSortNum(Integer.valueOf(sortNum));
    }
    String menuDesc = req.getParameter("menuDesc");
    if (!StringUtil.isEmpty(menuDesc)) {
      menuVo.setMenuDesc(menuDesc);
    }
    String menuId = req.getParameter("curid");
    if (!StringUtil.isEmpty(menuId)) {
      menuVo.setId(menuId);
    }

    String parentId = req.getParameter("parentId");
    if (!StringUtil.isEmpty(parentId)) {
      menuVo.setParentId(parentId);
    }*/
    String menuId = req.getParameter("curid");
    if (!StringUtil.isEmpty(menuId)) {
      menuVo.setId(menuId);
    }
    logger.info("MenuController menueEditData MenuVo:" + JSON.toJSONString(menuVo));

    try {

      logger.info("menuAdd start.");
      BaseResult baseResult = menuService.editMenu(menuVo);
      logger.info("menuAdd baseResult:" + JSON.toJSONString(baseResult));
      logger.info("menuAdd end.");

      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
        logger.info("DictRestController menuAdd end.");
        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
      }

      int count = (int) baseResult.getData();

      if (count == 0) {
        logger.info("MenuController menuAdd end.");
        return BaseResult.fail("0002", "编辑失败");
      } else if (count == -1) {
        logger.info("MenuController menuAdd end.");
        return BaseResult.fail("0003", "排序号重复");
      }

      logger.info("MenuController menueEditData end.");
      return BaseResult.success(count);

    } catch (Exception e) {
      logger.error("MenuController menueEditData error:" + e.getMessage());
      logger.info("MenuController menueEditData end.");
      return BaseResult.exception(e.getMessage());
    }

  }


  /**
   * 
   * menueEditData:菜单编辑保存
   * 
   * @param @param req
   * @param @return 设定文件
   * @return BaseResult DOM对象
   * @throws
   */
  @RequestMapping(value = {"/querySortNum"}, method = RequestMethod.POST)
  public @ResponseBody BaseResult querySortNum(HttpServletRequest req) {
    logger.info("MenuController querySortNum begin.");
    String parentId = req.getParameter("parentId");
    logger.info("MenuController querySortNum parentId:" + JSON.toJSONString(parentId));

    try {

      logger.info("getSortNum start.");
      BaseResult baseResult = menuService.getSortNumByParentId(parentId);
      logger.info("getSortNum baseResult:" + JSON.toJSONString(baseResult));
      logger.info("getSortNum end.");

      if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
        logger.info("DictRestController getSortNum end.");
        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
      }

      int count = (int) baseResult.getData();

      if (count <= 0) {
        logger.info("MenuController getSortNum end.");
        return BaseResult.fail("0002", "无数据");
      }

      logger.info("MenuController querySortNum end.");
      return BaseResult.success(count);

    } catch (Exception e) {
      logger.error("MenuController querySortNum error:" + e.getMessage());
      logger.info("MenuController querySortNum end.");
      return BaseResult.exception(e.getMessage());
    }

  }


  /**
   * 
   * delMenu:删除
   * 
   * @param @param req
   * @param @return 设定文件
   * @return BaseResult DOM对象
   * @throws
   */
  @RequestMapping(value = {"/delMenu"}, method = {RequestMethod.GET, RequestMethod.POST})
  public @ResponseBody BaseResult delMenu(HttpServletRequest req) {
    logger.info("MenuController delMenu begin.");
    String menuId = req.getParameter("menuId");
    String[] menuIdArr = null;
    if (!menuId.isEmpty()) {
      menuIdArr = menuId.split(",");
    }
    logger.info("MenuController delMenu menuId:" + JSON.toJSONString(menuIdArr));

    try {

      logger.info("delMenuByIdArr start.");
      BaseResult baseResult = menuService.delMenuByIdArr(menuIdArr);
      logger.info("delMenuByIdArr baseResult:" + JSON.toJSONString(baseResult));
      logger.info("delMenuByIdArr end.");
      if (baseResult.getRetCode().equals("0003")){
    	  logger.info("DictRestController delMenuByIdArr end.");
    	  return BaseResult.fail("0003", "菜单已分配，无法删除！");
      }
      else if (!baseResult.getRetCode().equals(ErrorCode.SuccessRetCode)) {
        logger.info("DictRestController delMenuByIdArr end.");
        return BaseResult.fail(ErrorCode.ExceptionRetCode, ErrorCode.ExceptionRetInfo);
      }

      int count = (int) baseResult.getData();

      if (count == 0) {
        logger.info("MenuController delMenuByIdArr end.");
        return BaseResult.fail("0002", "无数据");
      }
      else if (count == -1) {
          logger.info("MenuController menuAdd end.");
          return BaseResult.fail("0003", "菜单已分配，无法删除！");
        }

      logger.info("MenuController delMenu end.");
      return BaseResult.success(count);

    } catch (Exception e) {
      logger.error("MenuController delMenu error:" + e.getMessage());
      logger.info("MenuController delMenu end.");
      return BaseResult.exception(e.getMessage());
    }

  }

  /**
   * 
   * @Title: menuListByStaff
   * @Description: 获取员工可以看见的菜单

   * @param req
   * @return
   * @date: 2016年4月1日 下午1:35:36
   * @return: BaseResult
   */
  @RequestMapping(value = {"/menuListByStaff"}, method = {RequestMethod.GET, RequestMethod.POST})
  public @ResponseBody BaseResult menuListByStaff(HttpServletRequest req) {
    logger.info("MenuController menuListByStaff begin.");
    String staff_id = req.getParameter("staff_id");
    String sysinfo_id = req.getParameter("sysinfo_id");
    logger.info("MenuController menuListByStaff sysinfo_id:" + JSON.toJSONString(sysinfo_id));
    logger.info("MenuController menuListByStaff staff_id:" + JSON.toJSONString(staff_id));
    
    //TODO
//    staff_id = "cfbc0d15fc5740c3ac7d73716f063cd2";
//    sysinfo_id = "abc02bb906b348aabad2fb9b98fb9994";

    try {
      logger.info("getMenuByStaffPost begin.");
      BaseResult baseResult = menuService.getMenuByStaffPost(staff_id, sysinfo_id);
      logger.info("getMenuByStaffPost end.");
      logger.info("getMenuByStaffPost BaseResult:" + JSON.toJSONString(baseResult));
      logger.info("MenuController menuListByStaff end.");
      return baseResult;
    } catch (Exception e) {
      logger.error("MenuController menuListByStaff error:" + e.getMessage());
      logger.info("MenuController menuListByStaff end.");
      return BaseResult.exception(e.getMessage());
    }

  }
  

}
